package com.sgb.member.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;

import com.sgb.member.BR;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.ItemMemberPriceAdjustmentBinding;
import com.sgb.member.entity.PriceAdjustmentBean;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PointUtils;
import com.swgk.core.util.StringUtil;

import java.util.List;

public class PriceAdjustmentAdapter extends BaseRecycleAdapter<PriceAdjustmentBean> {
    private final int mCurPos;
    private final String mCurTitle;

    public PriceAdjustmentAdapter(Context context, List<PriceAdjustmentBean> datas, int position, String title) {
        super(context, R.layout.item_member_price_adjustment, datas);
        mCurPos = position;
        mCurTitle = title;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void convert(ItemViewHolder holder, PriceAdjustmentBean bean) {
        holder.setBinding(BR.itemBean, bean);
        ItemMemberPriceAdjustmentBinding binding = (ItemMemberPriceAdjustmentBinding) holder.getmBinding();
        binding.tvPriceType.setText(mCurTitle);
        if (!StringUtil.isEmpty(bean.getLastPrice())) {
            binding.tvLastPrice.setText(getFormatPrice(bean.getLastPrice(), bean.getUnit()));
        } else {
            if (!StringUtil.isEmpty(bean.getOriginalPrice())) {
                binding.tvLastPrice.setText(getFormatPrice(bean.getOriginalPrice(), bean.getUnit()));
            } else {
                binding.tvLastPrice.setText("/");
            }
        }
        binding.tvPrice.setText(getFormatPrice(String.valueOf(bean.getPrice()), bean.getUnit()));
        binding.tvDiffPrice.setText(getFormatDiffPrice(bean.getDifferencePrice(), bean.getUnit()));
        if (TextUtils.isEmpty(bean.getDifferencePrice()) || Double.parseDouble(bean.getDifferencePrice()) == 0) {
            binding.ivDiffPriceUnit.setVisibility(View.GONE);
            binding.tvDiffPricePrefix.setVisibility(View.GONE);
        } else {
            binding.ivDiffPriceUnit.setVisibility(View.VISIBLE);
            binding.tvDiffPricePrefix.setVisibility(View.VISIBLE);
            if (Double.parseDouble(bean.getDifferencePrice()) > 0) {
                binding.tvDiffPricePrefix.setText("+");
                binding.tvDiffPricePrefix.setTextColor(Color.parseColor("#FF4B10"));
                binding.ivDiffPriceUnit.setImageResource(R.drawable.icon_price_up);
            } else {
                binding.tvDiffPricePrefix.setText("");
                binding.tvDiffPricePrefix.setTextColor(Color.parseColor("#06C764"));
                binding.ivDiffPriceUnit.setImageResource(R.drawable.icon_price_down);
            }
        }
        binding.tvCompanyName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(bean.getCompanyNo())) {
                    MemberConfig.getInstance().startWebsiteInfo(v.getContext(), bean.getCompanyNo(), MemberConfig.WEBSITE_FOR_COMPANY);
                } else {
                    MToast.showToast(v.getContext(), "企业No为空");
                }
            }
        });
    }

    private SpannableString getFormatPrice(String doublePrice, String unit) {
        String price;
        if (TextUtils.isEmpty(doublePrice)) {
            price = "0.00";
        } else {
            price = PointUtils.price(Double.parseDouble(doublePrice));
        }
        if (price.length() > 6) {
            price = price.substring(0, 6) + "...";
        }
        String sUnit;
        // 商品带元
        if (TextUtils.isEmpty(unit)) {
            sUnit = mCurPos == 1 ? "" : " 元/";
        } else {
            sUnit = mCurPos == 1 ? "" + unit : " 元/" + unit;
        }
        String s = price + sUnit;
        int index = price.length();
        SpannableString sStr = new SpannableString(s);
        sStr.setSpan(new AbsoluteSizeSpan(15, true), 0, index, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        sStr.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗体
        sStr.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sStr.setSpan(new AbsoluteSizeSpan(10, true), index, s.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        sStr.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), index, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sStr;
    }

    private SpannableString getFormatDiffPrice(String doublePrice, String unit) {
        String price;
        if (TextUtils.isEmpty(doublePrice)) {
            price = "0.00";
        } else {
            price = PointUtils.price(Double.parseDouble(doublePrice));
        }
        if (price.length() > 6) {
            price = price.substring(0, 6) + "...";
        }
        // 商品带元
        String sUnit;
        if (TextUtils.isEmpty(unit)) {
            sUnit = mCurPos == 1 ? "" : " 元/";
        } else {
            sUnit = mCurPos == 1 ? "" + unit : " 元/" + unit;
        }
        int index = price.length();
        String s;
        int lastColor;
        if (TextUtils.isEmpty(doublePrice) || Double.parseDouble(doublePrice) == 0) {
            lastColor = Color.parseColor("#999999");
            s = price + sUnit;
        } else {
            if (Double.parseDouble(doublePrice) > 0) {
                s = price;
                lastColor = Color.parseColor("#FF4B10");
            } else {
                s = price;
                lastColor = Color.parseColor("#06C764");
            }
        }
        SpannableString sStr = new SpannableString(s);
        if (TextUtils.isEmpty(doublePrice) || Double.parseDouble(doublePrice) == 0) {
            sStr.setSpan(new AbsoluteSizeSpan(15, true), 0, index, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            sStr.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗体
            sStr.setSpan(new ForegroundColorSpan(lastColor), 0, index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            sStr.setSpan(new AbsoluteSizeSpan(10, true), index, s.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            sStr.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), index, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            sStr.setSpan(new AbsoluteSizeSpan(15, true), 0, s.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            sStr.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗体
            sStr.setSpan(new ForegroundColorSpan(lastColor), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return sStr;
    }
}
